<?php
// 接口API**********************
//username 用户名
// 接口API**********************
//获取传入的参数
// $username = $_REQUEST['username'];
// $password = $_REQUEST['password'];
$name = $_REQUEST['name'];
$way = $_REQUEST['way'];
$goodsname = $_REQUEST['goodsname'];
$bigNav = $_REQUEST['bigNav'];
$midNav = $_REQUEST['midNav'];
$smaNav = $_REQUEST['smaNav'];
$mat = $_REQUEST['mat'];
$price1 = $_REQUEST['price1'];
$price2 = $_REQUEST['price2'];
$id = $_REQUEST['id'];
//建立数据库连接
$link = mysqli_connect('localhost','root','123456','twoStage');
//判断连接成功与否
if(!$link){
    die('连接失败'.mysqli_connect_error());
};
//设置编码
mysqli_set_charset($link,'utf8');
if($way=='searchall'){
//创建sql语句，找出目标账号
$search = "select * from goods where shop='$name'";
//执行sql语句
$searchDo = mysqli_query($link,$search);
$returnArr = [];
while($row = mysqli_fetch_assoc($searchDo)){
    $returnArr[] = $row;
}
if(empty($returnArr)){
    echo json_encode($returnArr);
}else{
    echo json_encode($returnArr);
}
}else if($way=='addgoods'){
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp); 
    $_FILES["file"]["name"] = time().".".$extension;
    $url = './imgs/'.$_FILES["file"]["name"];
    $price1 = "￥".$price1;
    $price2 = "￥".$price2;
    $addgoods = "INSERT INTO goods (bigNav, midNAv, smaNav,goodsname,mat,url,price1,price2,shop) VALUES ('$bigNav', '$midNav', '$smaNav','$goodsname','$mat','$url','$price1','$price2','$name')";
    if (mysqli_query($link,$addgoods)) {
        // echo "新记录插入成功";
        move_uploaded_file($_FILES["file"]["tmp_name"], "../imgs/" . $_FILES["file"]["name"]);
    } ;
}else if($way=='update'){
    $price1 = "￥".$price1;
    $price2 = "￥".$price2;
    $update1 = "UPDATE goods SET goodsname='$goodsname', bigNav='$bigNav', midNav='$midNav', smaNav='$smaNav',mat='$mat', price1='$price1', price2='$price2' WHERE id=$id";
    if(mysqli_query($link,$update1)){
    }
}else if($way=='delete'){
    $delete = "DELETE FROM goods WHERE id=$id";
    if(mysqli_query($link,$delete)){
        echo 'ok';
    }
} 

//关闭数据库连接
mysqli_close($link);
?>